
#include "nane/SoundSystem/SoundSource.h"
#include "nane/SoundSystem/SoundSourceImpl_.h"
#include "nane/SoundSystem/SoundSystemImpl_.h"
#include "nane/SoundSystem/SoundBufferImpl_.h"

namespace nane 
{
    ////////////////////////////////////////////////////////////////////////////////////////////////
    SoundSource::SoundSource( const NullType& )
        : SubSystemProduct()
        , m( NULL )
    {
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
    SoundSource::SoundSource( Implementation* _impl )
        : SubSystemProduct( _impl )
        , m( _impl )
    {
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
    SoundSource::SoundSource( const SoundSource& _other )
        : SubSystemProduct( _other )
        , m( _other.m )
    {
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
    bool SoundSource::QueueBuffer( SoundBuffer& _soundBuffer )
    {
        if( _soundBuffer == NullPtr )
        {
            return false;
        }
        if( m->creator->interface->QueueSourceBuffer(m->interface, _soundBuffer.m->interface) == false )
        {
            return false;
        }
        m->lengthMs += _soundBuffer.GetLengthMs();
        m->buffers.push_back(_soundBuffer);
        return true;
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
    void SoundSource::Play()
    {
        if( m->state == Implementation::EState::Stopped )
        {
            m->state = Implementation::EState::PendingPlay;
            m->creator->RegisterUpdate(m);
        }
        else if( m->state == Implementation::EState::PendingStop )
        {
            m->state = Implementation::EState::Playing;
        }
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
    void SoundSource::Stop()
    {
        if( m->state == Implementation::EState::Playing )
        {
            m->state = Implementation::EState::PendingStop;
        }
        else if( m->state == Implementation::EState::PendingPlay )
        {
            m->state = Implementation::EState::Stopped;
            m->creator->UnregisterUpdate(m);
        }
        m->positionMs = 0;
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
}   // namespace nane
